package com.yida.wy.common.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdcardUtil;
import cn.hutool.core.util.PhoneUtil;
import com.yida.common.core.exception.ServiceException;
import com.yida.common.core.utils.StringUtils;
import com.yida.common.core.utils.poi.ExcelUtil;
import com.yida.wy.common.domain.WyBaseHouse;
import com.yida.wy.common.domain.WyBaseOwner;
import com.yida.wy.common.domain.dto.WyBaseOwnerHouseListDTO;
import com.yida.wy.common.domain.request.WyBaseOwnerUpdateRequest;
import com.yida.wy.common.domain.vo.WyBaseOwnerExcelVO;
import com.yida.wy.common.mapper.WyBaseHouseMapper;
import com.yida.wy.common.mapper.WyBaseOwnerMapper;
import com.yida.wy.common.service.IWyBaseOwnerService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * 基础数据（业主）Service业务层处理
 *
 * @author lqq
 * @date 2022-11-14
 */
@Service
public class WyBaseOwnerServiceImpl extends BasicServiceImpl<WyBaseOwnerMapper, WyBaseOwner> implements IWyBaseOwnerService {

    @Resource
    private WyBaseOwnerMapper ownerMapper;

    @Resource
    private WyBaseHouseMapper houseMapper;

    /**
     * 获取业务业主集合
     *
     * @param houseCodes 房屋标识码集合
     * @param areaId     地区ID
     * @return /
     */
    @Override
    public List<WyBaseOwner> selectGwjBaseListByGwjBaseHouseCodes(String[] houseCodes, Long areaId) {
        return ownerMapper.selectGwjBaseListByGwjBaseHouseCodes(houseCodes, areaId);
    }

    /**
     * 查询基础数据（业主）
     *
     * @param houseCode 房屋标识码
     * @param areaId    地区ID
     */
    @Override
    public WyBaseOwner selectGwjBaseOwnerByAreaIdAndHouseCode(String houseCode, Long areaId) {
        return ownerMapper.selectGwjBaseByGwjBaseHouseCodeAndAreaId(houseCode, areaId);
    }

    /**
     * 新增基础数据（业主）
     *
     * @param owner 基础数据（业主）
     * @return 结果
     */
    @Override
    public int insertGwjBaseOwner(WyBaseOwner owner) {
        return ownerMapper.insertGwjBaseOwner(owner);
    }

    /**
     * 修改基础数据（业主）
     *
     * @param owner 基础数据（业主）
     * @return 结果
     */
    @Override
    public int updateGwjBaseOwner(WyBaseOwner owner) {
        return ownerMapper.updateGwjBaseOwner(owner);
    }

    /************************* 新功能保留区 ************************************/

    @Override
    public List<WyBaseOwner> selectGwjBaseOwnerList(WyBaseOwner owner) {
        return ownerMapper.selectGwjBaseOwnerList(owner);
    }

    @Override
    public int insertImportGwjBaseOwner(List<WyBaseOwner> owners) {
        return ownerMapper.insertImportGwjBaseOwner(owners);
    }

    @Override
    public int updateImportGwjBaseOwner(List<WyBaseOwner> owners) {
        return ownerMapper.updateImportGwjBaseOwner(owners);
    }

    /************************* 新功能保留区 ************************************/

    /**
     * 修改基础数据（业主）
     *
     * @param request 基础数据（业主）
     * @return 结果
     */
    @Override
    public int updateGwjBaseOwnerByHouseId(WyBaseOwnerUpdateRequest request) {
        if (StringUtils.isAllBlank(request.getCardNum(), request.getOwnerName(), request.getOwnerPhone())) {
            throw new ServiceException("请输入业主信息");
        }
        // 查询房屋下是否有业主
        WyBaseHouse gwjBaseHouse = houseMapper.selectGwjBaseHouseByGwjBaseHouseId(request.getHouseId());
        if (StringUtils.isNull(gwjBaseHouse)) {
            throw new ServiceException("查无房屋信息");
        }
        if (StringUtils.isNotBlank(request.getOwnerPhone()) && !PhoneUtil.isPhone(request.getOwnerPhone())) {
            throw new ServiceException("请输入正确手机号码且只能输入一个");
        }
        if (StringUtils.isNotBlank(request.getCardNum()) && !IdcardUtil.isValidCard(request.getCardNum())) {
            throw new ServiceException("请输入正确身份证号码");
        }
        // 查询业主
        WyBaseOwner gwjBaseOwner = ownerMapper.selectGwjBaseByGwjBaseHouseCodeAndAreaId(gwjBaseHouse.getHouseCode(), gwjBaseHouse.getAreaId());
        WyBaseOwner newGwjBaseOwner = BeanUtil.copyProperties(request, WyBaseOwner.class);
        newGwjBaseOwner.setAreaId(gwjBaseHouse.getAreaId());
        newGwjBaseOwner.setStreetId(gwjBaseHouse.getStreetId());
        newGwjBaseOwner.setHouseCode(gwjBaseHouse.getHouseCode());
        if (StringUtils.isNull(gwjBaseOwner)) {
            // 做新增
            return insertGwjBaseOwner(newGwjBaseOwner);
        } else {
            // 修改
            newGwjBaseOwner.setOwnerId(gwjBaseOwner.getOwnerId());
            return updateGwjBaseOwner(newGwjBaseOwner);
        }
    }

    /**
     * 导入投票业主信息
     *
     * @param file        业主文件
     * @param communityId 小区ID
     * @return /
     * @throws Exception /
     */
    @Override
    public String importGwjBaseOwner(MultipartFile file, Long communityId) throws Exception {
        ExcelUtil<WyBaseOwnerExcelVO> util = new ExcelUtil<>(WyBaseOwnerExcelVO.class);
        List<WyBaseOwnerExcelVO> gwjBaseOwnerExcelVos = util.importExcel(file.getInputStream());
        if (StringUtils.isEmpty(gwjBaseOwnerExcelVos)) {
            throw new ServiceException("导入失败,导入业主不能为空！");
        }
        int successNum = 0;
        for (WyBaseOwnerExcelVO vo : gwjBaseOwnerExcelVos) {
            if (StringUtils.isNull(vo.getHouseId())) {
                continue;
            }
            // 查询房屋是否存在
            WyBaseHouse gwjBaseHouse = houseMapper.selectGwjBaseHouseByGwjBaseHouseId(vo.getHouseId());
            if (StringUtils.isNull(gwjBaseHouse)) {
                continue;
            }
            // 查询房屋下是否有业主
            WyBaseOwner gwjBaseOwner = ownerMapper.selectGwjBaseByGwjBaseHouseCodeAndAreaId(gwjBaseHouse.getHouseCode(), gwjBaseHouse.getAreaId());
            WyBaseOwner newGwjBaseOwner = BeanUtil.copyProperties(vo, WyBaseOwner.class);
            newGwjBaseOwner.setAreaId(gwjBaseHouse.getAreaId());
            newGwjBaseOwner.setStreetId(gwjBaseHouse.getStreetId());
            newGwjBaseOwner.setHouseCode(gwjBaseHouse.getHouseCode());
            // 校验格式
            if (!PhoneUtil.isPhone(newGwjBaseOwner.getOwnerPhone())) {
                throw new ServiceException("请输入正确手机号码且只能输入一个");
            }
            if (!IdcardUtil.isValidCard(newGwjBaseOwner.getCardNum())) {
                throw new ServiceException("请输入正确身份证号码");
            }
            if (StringUtils.isNull(gwjBaseOwner)) {
                // 做新增
                if (1 != insertGwjBaseOwner(newGwjBaseOwner)) {
                    throw new ServiceException("导入失败,新增业主数据失败");
                }
            } else {
                // 修改
                newGwjBaseOwner.setOwnerId(gwjBaseOwner.getOwnerId());
                if (1 != updateGwjBaseOwner(newGwjBaseOwner)) {
                    throw new ServiceException("导入失败,更新业主数据失败");
                }
            }
            successNum++;
        }
        return "恭喜您，成功导入" + successNum + " 条数据";
    }

    @Override
    public List<WyBaseOwnerHouseListDTO> selectGwjOwnerHouseList(WyBaseOwner gwjBaseOwner) {
        return ownerMapper.selectGwjOwnerHouseList(gwjBaseOwner);
    }
}
